package org.colin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.colin.dictPojo.DLanguage;
import org.colin.dictPojo.DStudyLevel;
import org.colin.mapper.DEntryMapper;
import org.colin.mapper.DLanguageMapper;
import org.colin.mapper.DStudyLevelMapper;
import org.colin.service.DEntryService;
import org.colin.service.DLanguageService;
import org.colin.service.DStudyLevelService;
import org.colin.utils.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 *  缓存类
 *  CommandLineRunner  : 此接口是在Spring boot 启动之后  执行run方法，  将索引数据缓存进数据库，会先进行判断，缓存中有这个数据没。
 *  俗名：缓存预热
 *
 */
//@Component
class MyStartupRunner implements CommandLineRunner {


    @Autowired
    DEntryMapper dEntryMapper ;
    @Autowired
    DLanguageMapper dLanguageMapper;
    @Autowired
    DStudyLevelService dStudyLevelService;


    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public void run(String... args) throws Exception {
        // 拼音索引名
        //语种
        // 缓存目前缓存的是汉外类型的词头数据
        QueryWrapper<DLanguage> dLanguageQueryWrapper = new QueryWrapper<>();
        dLanguageQueryWrapper.eq("type",1);
        List<DLanguage> languageList = dLanguageMapper.selectList(dLanguageQueryWrapper);
        //级别
        List<DStudyLevel> levelList = dStudyLevelService.list();
        // 首字母索引
        for (int i = 0; i < languageList.size(); i++) {
            Integer languageId = languageList.get(i).getId();
            String tableName = ConfigCode.getEntryTableName(languageId);
            for (int j = 0; j < levelList.size(); j++) {
                Integer levelId = levelList.get(j).getId();
                for (int k = 0; k < ConfigCode.arr.length; k++) {
                    String sp = ConfigCode.arr[k];
                    //例 ： pinyinIndex_1_1_a  代表 语种ID为1的  学习等级为1的 首字母为a 的所有单词
                    String pinyinIndexs = ConfigCode.getEntryPinYinIndex(languageId,levelId,sp);
                    Long size = redisTemplate.boundListOps(pinyinIndexs).size();
                    //判断缓存内是否存在，存在再存入。
                    //必须限定好，缓存内数据，只有两个地方可以存入。
                    // 1：此接口，项目启动时存入
                    // 2：增加词头时与修改词头时  注：修改时，需要将该拼音对应的缓存清空，重新录入数据。
                    if(size == 0) {
                        //接口效率必须要高
                        List<String> list = dEntryMapper.selectEntryGroupByPinYinIndex(tableName, levelId, sp);
                        redisTemplate.boundListOps(pinyinIndexs).rightPushAll(list);
                    }

                    HashMap<String, Object> hashMap = new HashMap<>();
                    String entryPinYinDetailIndex = ConfigCode.getEntryPinYinDetailIndex(languageId, levelId, pinyinIndexs);

                    Long sizes = redisTemplate.boundHashOps(entryPinYinDetailIndex).size();
                    if(sizes <= 0){
                        List<String> strings = dEntryMapper.selectPinYinBySD(tableName, levelId, pinyinIndexs);
                        for (int z = 0; z < strings.size(); z++) {
                            String pinyin1 = strings.get(z);
                            List<HashMap> list = dEntryMapper.selectEntryDetailByPinYin(tableName, levelId, pinyin1);
                            if (list.size() > 0) {
                                hashMap.put(pinyin1, list);
                            }
                        }
                        redisTemplate.boundHashOps(entryPinYinDetailIndex).putAll(hashMap);
                    }
                }
            }
        }

        //笔画索引
        for (int i = 0; i < languageList.size(); i++) {
            Integer languageId = languageList.get(i).getId();
            String tableName = ConfigCode.EntryTaleName + languageId;
            for (int j = 0; j < levelList.size(); j++) {
                Integer levelId = levelList.get(j).getId();
                String entryStrokesIndex = ConfigCode.getEntryStrokesIndex(languageId,levelId);
                Long size1 = redisTemplate.boundListOps(entryStrokesIndex).size();
                if(size1 <= 0) {
                    List<Integer> list = dEntryMapper.selectEntryGroupByStrokesIndex(tableName, levelId);
                    redisTemplate.boundListOps(entryStrokesIndex).rightPushAll(list);
                    for (int k = 0; k < list.size(); k++) {
                        Integer strokes = list.get(k);
                        String entryStrokesDetailIndex = ConfigCode.getEntryStrokesDetailIndex(languageId, levelId, strokes);
                        Long size = redisTemplate.boundListOps(entryStrokesDetailIndex).size();
                        if(size == 0) {
                            List<Map> data = dEntryMapper.selectEntryByStrokesIndex(tableName, levelId, strokes);
                            redisTemplate.boundListOps(entryStrokesDetailIndex).rightPushAll(data);
                        }
                    }
                }
            }
        }
    }
}